#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, fa[5001];
struct Node {
	ll x, y, v;
	bool operator < (Node& A) {
		return v < A.v;
	}
} a[8001]; 

ll findset(ll i) {
	if (fa[i] == i)
		return i;
	return fa[i] = findset(fa[i]);
}

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1; i <= m; ++i)
		scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].v);
	sort(a + 1, a + 1 + m);
	ll ans = 1 << 30;
	for (ll i = 1; i <= n; ++i) {
		for (ll j = 1; j <= m; ++j)
			fa[j] = j;
		ll cnt = n;
		for (ll j = i; j <= m; ++j) {
			ll x = findset(a[j].x), y = findset(a[j].y);
			if (x != y) {
				fa[x] = y;
				cnt--;
			}
			if (cnt == 1)
				ans = min(ans, a[j].v - a[i].v);
		}
	}
	printf("%lld\n", ans);
	return 0;
}
